package com.helloandroid.paso.ui.social;

import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.ImageButton;
import android.widget.ProgressBar;

public class Social {
	
	private String facebookURL, myspaceURL, iwiwURL;
	private int facebookButtonID, myspaceButtonID, iwiwButtonID;
	private WebView wview;
	private ProgressBar pbar;
	
	/**
	 * 
	 * @param webView 
	 * @param progressBar 
	 * @param facebookButton
	 * @param myspaceButton
	 * @param iwiwButton
	 * @param facebookURL
	 * @param myspaceURL
	 * @param iwiwURL
	 */
	public void InitSocial(WebView webView, ProgressBar progressBar,
			ImageButton facebookButton, ImageButton myspaceButton,
			ImageButton iwiwButton, String facebookURL, String myspaceURL,
			String iwiwURL) {
		
		//Init private variables
		this.facebookURL = facebookURL;
		this.myspaceURL = myspaceURL;
		this.iwiwURL = iwiwURL;

		facebookButtonID = facebookButton.getId();
		myspaceButtonID = myspaceButton.getId();
		iwiwButtonID = iwiwButton.getId();

		wview = webView;
		pbar = progressBar;
		
		//Set custom WebViewClient to prevent redirects from opening the browser
		wview.setWebViewClient(new CustomWebviewClient());

		// Set chrome client, define onProgressChanged to display progress
		wview.setWebChromeClient(new WebChromeClient() {
			public void onProgressChanged(WebView view, int progress) {
				pbar.setVisibility(0);
				pbar.setProgress(progress);

				if (progress == 100)
					pbar.setVisibility(8);
			}
		});
		
		//Set button click listener
		facebookButton.setOnClickListener(socialButtonsClickListener);
		myspaceButton.setOnClickListener(socialButtonsClickListener);
		iwiwButton.setOnClickListener(socialButtonsClickListener);
		
		wview.loadUrl(this.myspaceURL);
	}

	private OnClickListener socialButtonsClickListener = new OnClickListener() {
		public void onClick(View v) {
			int id = v.getId();

			if (id == facebookButtonID)
				wview.loadUrl(facebookURL);
			else if (id == myspaceButtonID)
				wview.loadUrl(myspaceURL);
			else if (id == iwiwButtonID)
				wview.loadUrl(iwiwURL);
		}
	};
}
